package com.atguigu.staservice.schedule;

import com.atguigu.staservice.service.StatisticsDailyService;
import com.atguigu.staservice.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.unit.DataUnit;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: 亚当
 * @date: 2022年07月15日 15:38
 * @package: com.atguigu.staservice.schedule
 * @Description: 定时任务
 */
@Component
public class ScheduledTask {

    @Autowired
    private StatisticsDailyService staService;
    /**
     * 测试
     * 每天七点到二十三点每五秒执行一次
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void test1(){
        System.out.println("test1执行了");
        SimpleDateFormat month = new SimpleDateFormat("YYYY-MM-dd:HH:mm:ss");
        String format = month.format(new Date());
        System.out.println("时间："+format);
    }
    /**
     * 每天凌晨1点执行一次,把前一天数据进行数据添加
     */
    @Scheduled(cron = "0 0 1 * * ? ")
    public void test2(){
        staService.registerCount(DateUtil.formatDate(DateUtil.addDays(new Date(),-1)));

    }
    @Test
    public void test(){
        Date date = DateUtil.addDays(new Date(), -2);
        System.out.println(date);
        String s = DateUtil.formatDate(date);
        System.out.println(s);
    }
}
